<template>
	<div id="top">
		<the-header-top></the-header-top>
		<logo-bar></logo-bar>
		
		<div class="identity-authentication">
			<div class="identity-authentication-container" v-if="type === 'set' || type === 'update' || type === 'change'">
				<!--路径-->
				<ul class="breadcrumb">
					<li><router-link to="/">{{ $t('message.home') }}</router-link></li>
					<li><router-link to="/personal">{{$t('message.personalCenter')}}</router-link></li>
					<li><router-link to="/personal/identity">{{$t('message.identityAuthentication')}}</router-link></li>
					<li>{{ pageText[attr].breadcrumb }}</li>
				</ul>
				<!--申请身份认证-->
				<div class="authentication">
					<!--标题-->
					<div class="authentication-title">{{ pageText[attr].title }}</div>
          <!--进度条-->
          <v-progress-bar :length="3" :active="progress" class="identity-progress-bar"></v-progress-bar>
          <ul class="identity-progress-text">
            <li>{{ $t('message.PersonalIdentityAuthenticationFillIdentityInfo') }}</li>
            <li>{{ $t('message.PersonalIdentityAuthenticationFillStudentInfo') }}</li>
            <li>{{ $t('message.PersonalIdentityAuthenticationSubmitCertification') }}</li>
          </ul>
          <!--认证失败消息提示-->
					<div class="authentication-warning" v-if="(type === 'update' || type === 'change') && progress === 1">
						{{ warningIden }}
					</div>
					<div class="authentication-warning" v-else-if="(type === 'update' || type === 'change') && progress === 2">
						{{ warningStud }}
					</div>
					<!--上传-->
					<div class="authentication-content">
            <!--身份验证组件set-->
            <v-identity class="v-identity-component" v-if="type === 'set' && progress === 1" @change-params="getIdentityInfo"></v-identity>
            <!--学校验证组件set-->
            <v-student class="v-student-component" v-else-if="type === 'set' && progress === 2" @change-params="getStudentInfo"></v-student>
            <!--身份验证组件change || update-->
            <v-identity class="v-identity-component" v-else-if="(type === 'change' || type === 'update') && progress === 1" @change-params="getIdentityInfo" :parentData="identityInfo"></v-identity>
            <!--学校验证组件change || update-->
            <v-student class="v-student-component" v-else-if="(type === 'change' || type === 'update') && progress === 2" @change-params="getStudentInfo" :parentData="studentInfo"></v-student>
            <!--上传身份验证信息成功-->
            <div class="change-success" v-if="progress === 3">
              <div class="change-success-icon"></div>
              <div class="change-success-title">
                {{$t('message.submitIdentityConfirmSuccess')}}！
              </div>
              <div class="change-success-tip">
                {{ delayTime }}{{$t('message.backToIdentityConfirm')}}
              </div>
            </div>
					</div>
					<!--提交按钮-->
					<div class="btn-submit">
            <!--下一步-->
            <template v-if="progress === 1">
              <!-- set -->
              <template v-if="type === 'set'">
                <!-- changeStatus === 3 -->
                <template v-if="setType === 'studExists'">
                  <button type="button" @click="onSubmit" v-if="allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
                  <button type="button" class="btn-forbid-item" v-else-if="!allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
                </template>
                <!-- else -->
                <template v-else-if="setType === 'empty'">
                  <button type="button" @click="onNext" v-if="allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button>
                  <button type="button" class="btn-forbid-item" v-else-if="!allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button>
                </template>
              </template>
              <!-- change -->
              <template v-else-if="type === 'change'">
                <!-- changeStatus === 3 -->
                <template v-if="changeStatus === 3">
                  <button type="button" @click="onSubmit" v-if="allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
                  <button type="button" class="btn-forbid-item" v-else-if="!allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
                </template>
                <!-- else -->
                <template v-else>
                  <button type="button" @click="onNext" v-if="allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button>
                  <button type="button" class="btn-forbid-item" v-else-if="!allowSubmitIden">{{$t('message.PersonalIdentityAuthenticationNextBtn')}}</button>
                </template>
              </template>
            </template>
            <!--提交-->
            <template v-if="progress === 2">
              <button type="button" @click="onSubmit" v-if="allowSubmitStud">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
              <button type="button" class="btn-forbid-item" v-else-if="!allowSubmitStud">{{$t('message.PersonalIdentityAuthenticationSubmitBtn')}}</button>
            </template>
					</div>
				</div>
			</div>
		</div>
	</div>
</template>

<script>
import TheHeaderTop from '@/components/base/TheHeaderTop'
import LogoBar from '@/components/base/LogoBar'
import VUploadImage from '@/components/base/VUploadImage'
import VProgressBar from '@/components/base/VProgressBar'
import VIdentity from '@/components/base/VIdentity'
import VStudent from '@/components/base/VStudent'

export default {
  name: 'PersonalIdentityAuthentication',
  props: ['type'],
  components: {
    TheHeaderTop,
    LogoBar,
    VUploadImage,
    VProgressBar,
    VIdentity,
    VStudent
  },
  data () {
    return {
      reason: this.$t('message.reason'),
      // 进度
      progress: 1,
      // 页面信息
      pageText: {
        set: {
          breadcrumb: this.$t('message.applyIdentify'),
          title: this.$t('message.identityAuthentication')
        },
        update: {
          breadcrumb: this.$t('message.updateIdentify'),
          title: this.$t('message.updateIdentify'),
          warning: ''
        },
        change: {
          breadcrumb: this.$t('message.editIdentify'),
          title: this.$t('message.identityAuthentication'),
          warning: this.$t('message.warning')
        }
      },
      // 后台加载过来验证信息的id
      identityInfoID: '',
      studentInfoID: '',
      // 后台加载过来的验证信息
      initIdentityInfo: {},
      initStudentInfo: {},
      // 身份验证信息
      identityInfo: {},
      // 学校验证信息
      studentInfo: {},
      // 跳转延迟时间
      delayTime: 3,
      // 是否允许提交
      allowSubmitIden: false,
      allowSubmitStud: false,
      // 认证失败原因
      warningIden: this.$t('message.pleaseFillIdenInfo'),
      warningStud: this.$t('message.pleaseFillStudInfo'),
      /**
       * changeStatus
       * 1: 身份认证失败 & 学校认证未上传
       * 2: 身份认证未上传 & 学校认证失败
       * 3: 身份认证失败 & 学校认证成功
       * 4: 身份认证成功 & 学校认证失败
       * 5: 身份认证失败 & 学校认证失败
       */
      changeStatus: 5
    }
  },
  computed: {
    attr () {
      if (this.type === 'set' || this.type === 'update' || this.type === 'change') {
        return this.type
      } else {
        return 'other'
      }
    },
    // 身份认证 or 学校认证已上传
    setType () {
      return this.$route.query.setType
    }
  },
  methods: {
    // 提交验证
    onSubmit () {
      // 新提交身份认证set
      if (this.type === 'set') {
        // 获取需要上传的数据
        let data = {
          // 身份验证
          id_card_front_side_url: this.identityInfo.frontOfIDCard,
          surname: this.identityInfo.lastName,
          name: this.identityInfo.firstName,
          id_num: this.identityInfo.identityNumber,
          birth_date: this.identityInfo.birthDate,
          sex: this.identityInfo.sex = this.identityInfo.sex === 'female' ? 0
                                : this.identityInfo.sex === 'male' ? 1
                                : -1,
          id_address: this.identityInfo.identityAddress,
          id_start: this.identityInfo.startDate,
          id_end: this.identityInfo.endDate,
          now_address: this.identityInfo.currentAddress,
          // 学校验证
          student_front_side_url: this.studentInfo.photo,
          school_name: this.studentInfo.school,
          school_district: this.studentInfo.schoolDistrict,
          education: this.studentInfo.education,
          college: this.studentInfo.institute,
          major: this.studentInfo.major,
          grade: this.studentInfo.grade,
          studentID: this.studentInfo.studentID,
          entry_date: this.studentInfo.enrolledDate,
          student_card_type: this.studentInfo.photoSource === 'studentCard' ? 1 : this.studentInfo.photoSource === 'campusNetwork' ? 2 : -1,
          student_card_start: this.studentInfo.startDate,
          student_card_end: this.studentInfo.endDate
        }
        let idenData = {
          id_card_front_side_url: this.identityInfo.frontOfIDCard,
          surname: this.identityInfo.lastName,
          name: this.identityInfo.firstName,
          id_num: this.identityInfo.identityNumber,
          birth_date: this.identityInfo.birthDate,
          sex: this.identityInfo.sex = this.identityInfo.sex === 'female' ? 0
                                : this.identityInfo.sex === 'male' ? 1
                                : -1,
          id_address: this.identityInfo.identityAddress,
          id_start: this.identityInfo.startDate,
          id_end: this.identityInfo.endDate,
          now_address: this.identityInfo.currentAddress
        }
        let studData = {
          student_front_side_url: this.studentInfo.photo,
          school_name: this.studentInfo.school,
          school_district: this.studentInfo.schoolDistrict,
          education: this.studentInfo.education,
          college: this.studentInfo.institute,
          major: this.studentInfo.major,
          grade: this.studentInfo.grade,
          studentID: this.studentInfo.studentID,
          entry_date: this.studentInfo.enrolledDate,
          student_card_type: this.studentInfo.photoSource = this.studentInfo.photoSource === 'studentCard' ? 1 : this.studentInfo.photoSource === 'campusNetwork' ? 2 : -1,
          student_card_start: this.studentInfo.startDate,
          student_card_end: this.studentInfo.endDate
        }
        if (this.setType === 'idenExists') {
          this.setStud(studData)
        } else if (this.setType === 'studExists') {
          this.setIden(idenData)
        } else if (this.setType === 'empty') {
          this.$ajax.setIdentity(data).then(res => {
            // set 身份验证信息错误处理
            if (res.code === 200) {
              console.log('上传成功')
              this.progress = 3
              // 跳转到身份信息页面
              this.toIdentity()
            } else {
              console.log(res.code, res.msg)
            }
          })
        } else {
          console.log('Cannot get setType')
        }
      // 更新身份认证信息update || 更换身份认证信息change
      } else {
        var identityData = {
          user_verify: [{
            'id': this.identityInfoID,
            'file': this.initIdentityInfo.frontOfIDCard
          }]
        }
        var studentData = {
          user_verify: [{
            'id': this.studentInfoID,
            'file': this.initStudentInfo.photo
          }]
        }
        // 构造产生了修改的数据传入接口
        if (this.changeStatus === 1) {
          // 构造身份认证数据
          for (let key in this.identityInfo) {
            identityData.user_verify[0][this.getIdenKey(key)] = this.identityInfo[key]
          }
          identityData.user_verify[0].sex = identityData.user_verify[0].sex === 'female' ? 0
                                          : identityData.user_verify[0].sex === 'male' ? 1
                                          : -1
          // 判断是否需要设置新的学校认证信息
          if (JSON.stringify(this.studentInfo) === '{}') {
            this.updateIdenConfirm(identityData)
          } else {
            // 构造学校认证数据
            studentData = {
              student_front_side_url: this.studentInfo.photo,
              school_name: this.studentInfo.school,
              school_district: this.studentInfo.schoolDistrict,
              education: this.studentInfo.education,
              college: this.studentInfo.institute,
              major: this.studentInfo.major,
              grade: this.studentInfo.grade,
              studentID: this.studentInfo.studentID,
              entry_date: this.studentInfo.enrolledDate,
              student_card_type: this.studentInfo.photoSource = this.studentInfo.photoSource === 'studentCard' ? 1 : this.studentInfo.photoSource === 'campusNetwork' ? 2 : -1,
              student_card_start: this.studentInfo.startDate,
              student_card_end: this.studentInfo.endDate
            }
            this.updateIdenSetStud(identityData, studentData)
          }
        } else if (this.changeStatus === 2) {
          // 构造学校认证数据
          for (let key in this.studentInfo) {
            if (this.getStudKey(key) === 'education' || this.getStudKey(key) === 'school_district') {
              if (this.studentInfo[key] instanceof Object) {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key].name
              } else {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
              }
            } else {
              studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
            }
          }
          studentData.user_verify[0].student_card_type = studentData.user_verify[0].student_card_type === 'studentCard' ? 1 : studentData.user_verify[0].student_card_type === 'campusNetwork' ? 2 : -1
          if (JSON.stringify(this.identityInfo) === '{}') {
            this.updateStudConfirm(studentData)
          } else {
            // 构造身份认证数据
            identityData = {
              id_card_front_side_url: this.identityInfo.frontOfIDCard,
              surname: this.identityInfo.lastName,
              name: this.identityInfo.firstName,
              id_num: this.identityInfo.identityNumber,
              birth_date: this.identityInfo.birthDate,
              sex: this.identityInfo.sex = this.identityInfo.sex === 'female' ? 0
                                    : this.identityInfo.sex === 'male' ? 1
                                    : -1,
              id_address: this.identityInfo.identityAddress,
              id_start: this.identityInfo.startDate,
              id_end: this.identityInfo.endDate,
              now_address: this.identityInfo.currentAddress
            }
            // 提交更新
            this.setIdenUpdateStud(identityData, studentData)
          }
        } else if (this.changeStatus === 3) {
          // 构造数据
          for (let key in this.identityInfo) {
            identityData.user_verify[0][this.getIdenKey(key)] = this.identityInfo[key]
          }
          identityData.user_verify[0].sex = identityData.user_verify[0].sex === 'female' ? 0
                                          : identityData.user_verify[0].sex === 'male' ? 1
                                          : -1
          // 提交更新
          this.updateIdenConfirm(identityData)
        } else if (this.changeStatus === 4) {
          // 构造数据
          for (let key in this.studentInfo) {
            if (this.getStudKey(key) === 'education' || this.getStudKey(key) === 'school_district') {
              if (this.studentInfo[key] instanceof Object) {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key].name
              } else {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
              }
            } else {
              studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
            }
          }
          studentData.user_verify[0].student_card_type = studentData.user_verify[0].student_card_type === 'studentCard' ? 1 : studentData.user_verify[0].student_card_type === 'campusNetwork' ? 2 : -1
          // 提交更新
          this.updateStudConfirm(studentData)
        } else if (this.changeStatus === 5) {
          // 构造数据
          for (let key in this.identityInfo) {
            identityData.user_verify[0][this.getIdenKey(key)] = this.identityInfo[key]
          }
          for (let key in this.studentInfo) {
            if (this.getStudKey(key) === 'education' || this.getStudKey(key) === 'school_district') {
              if (this.studentInfo[key] instanceof Object) {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key].name
              } else {
                studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
              }
            } else {
              studentData.user_verify[0][this.getStudKey(key)] = this.studentInfo[key]
            }
          }
          identityData.user_verify[0].sex = identityData.user_verify[0].sex === 'female' ? 0
                                          : identityData.user_verify[0].sex === 'male' ? 1
                                          : -1
          studentData.user_verify[0].student_card_type = studentData.user_verify[0].student_card_type === 'studentCard' ? 1 : studentData.user_verify[0].student_card_type === 'campusNetwork' ? 2 : -1
          // 提交更新
          this.updateDoubleConfirm(identityData, studentData)
        } else {
          console.log('没有获取到错误状态')
        }
      }
    },
    // 下一步，前进到学校验证
    onNext () {
      this.progress = 2
      scrollTo(0, 0)
      history.pushState(null, null, location.href)
    },
    // 获取更新认证信息update
    getUpdateData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getDatedIndentity(languageType).then(res => {
        if (res.code === 200) {
          res.data.forEach((item, index) => {
            if (item.type === 'credit_identificationscanning') {
              this.initIdentityInfo = {
                firstName: item.surname,
                lastName: item.name,
                sex: item.sex === 0 ? 'female'
                   : item.sex === 1 ? 'male'
                   : '',
                identityNumber: item.id_num,
                birthDate: item.birth_date,
                identityAddress: item.id_address,
                startDate: item.id_start,
                endDate: item.id_end,
                currentAddress: item.now_address,
                frontOfIDCard: item.file
              }
              this.identityInfo = {
                firstName: item.surname,
                lastName: item.name,
                sex: item.sex === 0 ? 'female'
                   : item.sex === 1 ? 'male'
                   : '',
                identityNumber: item.id_num,
                birthDate: item.birth_date,
                identityAddress: item.id_address,
                startDate: item.id_start,
                endDate: item.id_end,
                currentAddress: item.now_address,
                frontOfIDCard: item.file
              }
              this.identityInfoID = item.id
            } else if (item.type === 'credit_graducation') {
              this.initStudentInfo = {
                school: item.school_name,
                schoolDistrict: item.school_district,
                education: item.education,
                institute: item.college,
                major: item.major,
                grade: item.grade,
                studentID: item.studentID,
                enrolledDate: item.entry_date,
                photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                startDate: item.student_card_start,
                endDate: item.student_card_end,
                photo: item.file
              }
              this.studentInfo = {
                school: item.school_name,
                schoolDistrict: item.school_district,
                education: item.education,
                institute: item.college,
                major: item.major,
                grade: item.grade,
                studentID: item.studentID,
                enrolledDate: item.entry_date,
                photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                startDate: item.student_card_start,
                endDate: item.student_card_end,
                photo: item.file
              }
              this.studentInfoID = item.id
            } else {
              console.log('getDatedIndentity()数据错误')
            }
          })
          console.log('Get update data')
        } else {
          console.log(res.code, res.msg)
        }
      })
    },
    // 获取修改认证信息change
    getChangeData () {
      let t = window.localStorage.getItem('thaiPayMallLanguage')
      var languageType
      if (t) {
        if (t === 'cn') {
          languageType = 1
        } else if (t === 'en') {
          languageType = 3
        } else {
          languageType = 2
        }
      } else {
        languageType = 2
      }
      this.$ajax.getFailIndentity(languageType).then(res => {
        if (res.code === 200) {
          if (res.data.length === 1) {
            res.data.forEach((item, index) => {
              if (item.type === 'credit_identificationscanning') {
                this.initIdentityInfo = {
                  firstName: item.name,
                  lastName: item.surname,
                  sex: item.sex === 0 ? 'female'
                    : item.sex === 1 ? 'male'
                    : '',
                  identityNumber: item.id_num,
                  birthDate: item.birth_date,
                  identityAddress: item.id_address,
                  startDate: item.id_start,
                  endDate: item.id_end,
                  currentAddress: item.now_address,
                  // frontOfIDCard: item.file
                  frontOfIDCard: ''
                }
                this.identityInfo = {
                  firstName: item.name,
                  lastName: item.surname,
                  sex: item.sex === 0 ? 'female'
                    : item.sex === 1 ? 'male'
                    : '',
                  identityNumber: item.id_num,
                  birthDate: item.birth_date,
                  identityAddress: item.id_address,
                  startDate: item.id_start,
                  endDate: item.id_end,
                  currentAddress: item.now_address,
                  // frontOfIDCard: item.file
                  frontOfIDCard: ''
                }
                this.identityInfoID = item.id
                this.warningIden = this.$t('message.theReason') + (!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
                // 身份认证失败 & 学校认证未上传
                this.changeStatus = 1
              } else if (item.type === 'credit_graducation') {
                this.initStudentInfo = {
                  school: item.school_name,
                  schoolDistrict: item.school_district,
                  education: item.education,
                  institute: item.college,
                  major: item.major,
                  grade: item.grade,
                  studentID: item.studentID,
                  enrolledDate: item.entry_date,
                  photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                  startDate: item.student_card_start,
                  endDate: item.student_card_end,
                  // photo: item.file
                  photo: ''
                }
                this.studentInfo = {
                  school: item.school_name,
                  schoolDistrict: item.school_district,
                  education: item.education,
                  institute: item.college,
                  major: item.major,
                  grade: item.grade,
                  studentID: item.studentID,
                  enrolledDate: item.entry_date,
                  photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                  startDate: !item.student_card_start || item.student_card_start === '0000/00/00' || item.student_card_start === '' ? null : item.student_card_start,
                  endDate: !item.student_card_end || item.student_card_end === '0000/00/00' || item.student_card_end === '' ? null : item.student_card_end,
                  // photo: item.file
                  photo: ''
                }
                this.studentInfoID = item.id
                this.warningStud = this.$t('message.educationFailedReason') + (!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
                // 身份认证未上传 & 学校认证失败
                this.changeStatus = 2
              } else {
                console.log('响应数据的type字段读取失败')
              }
            })
          } else if (res.data.length === 2) {
            // 判断两种信息是否通过，1: 成功 2: 失败
            let idenPassed = -1
            let studPassed = -1
            res.data.forEach((item, index) => {
              if (item.type === 'credit_identificationscanning') {
                this.initIdentityInfo = {
                  firstName: item.name,
                  lastName: item.surname,
                  sex: item.sex === 0 ? 'female'
                    : item.sex === 1 ? 'male'
                    : '',
                  identityNumber: item.id_num,
                  birthDate: item.birth_date,
                  identityAddress: item.id_address,
                  startDate: item.id_start,
                  endDate: item.id_end,
                  currentAddress: item.now_address,
                  // frontOfIDCard: item.file
                  frontOfIDCard: ''
                }
                this.identityInfo = {
                  firstName: item.name,
                  lastName: item.surname,
                  sex: item.sex === 0 ? 'female'
                    : item.sex === 1 ? 'male'
                    : '',
                  identityNumber: item.id_num,
                  birthDate: item.birth_date,
                  identityAddress: item.id_address,
                  startDate: item.id_start,
                  endDate: item.id_end,
                  currentAddress: item.now_address,
                  // frontOfIDCard: item.file
                  frontOfIDCard: ''
                }
                this.identityInfoID = item.id
                this.warningIden = this.$t('message.theReason') + (!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
                // 身份认证状态
                idenPassed = item.passed
              } else if (item.type === 'credit_graducation') {
                this.initStudentInfo = {
                  school: item.school_name,
                  schoolDistrict: item.school_district,
                  education: item.education,
                  institute: item.college,
                  major: item.major,
                  grade: item.grade,
                  studentID: item.studentID,
                  enrolledDate: item.entry_date,
                  photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                  startDate: item.student_card_start,
                  endDate: item.student_card_end,
                  // photo: item.file
                  photo: ''
                }
                this.studentInfo = {
                  school: item.school_name,
                  schoolDistrict: item.school_district,
                  education: item.education,
                  institute: item.college,
                  major: item.major,
                  grade: item.grade,
                  studentID: item.studentID,
                  enrolledDate: item.entry_date,
                  photoSource: item.student_card_type === 1 ? 'studentCard' : item.student_card_type === 2 ? 'campusNetwork' : 'undefined',
                  startDate: !item.student_card_start || item.student_card_start === '0000/00/00' || item.student_card_start === '' ? null : item.student_card_start,
                  endDate: !item.student_card_end || item.student_card_end === '0000/00/00' || item.student_card_end === '' ? null : item.student_card_end,
                  // photo: item.file
                  photo: ''
                }
                this.studentInfoID = item.id
                this.warningStud = this.$t('message.educationFailedReason') + (!item.msg || item.msg === '' ? this.$t('message.none') : item.msg)
                // 学校认证状态
                studPassed = item.passed
              } else {
                console.log('响应数据的type字段读取失败')
              }
            })
            // 总结 changeStatus
            if (idenPassed === 2 && studPassed === 1) { // 身份认证失败 & 学校认证成功
              this.progress = 1
              this.changeStatus = 3
            } else if (idenPassed === 1 && studPassed === 2) { // 身份认证成功 & 学校认证失败
              this.progress = 2
              this.changeStatus = 4
            } else if (idenPassed === 2 && studPassed === 2) { // 身份认证失败 & 学校认证失败
              this.progress = 1
              this.changeStatus = 5
            }
          }
          console.log('Get change data')
        } else if (res.code === 301) {
          console.log('Sign in again')
        } else {
          console.log('Something is wrong')
        }
      })
    },
    // 跳转到身份信息页面
    toIdentity () {
      scrollTo(0, 485)
      this.delayTimer = setInterval(() => {
        if (this.delayTime <= 0) {
          clearInterval(this.delayTimer)
          this.$router.push({ path: '/personal/identity' })
        } else {
          this.delayTime -= 1
        }
      }, 1000)
    },
    // 从身份验证子组件获取数据
    getIdentityInfo (data) {
      if (data === 'no') {
        this.allowSubmitIden = false
      } else {
        this.identityInfo = data
        this.allowSubmitIden = true
      }
    },
    // 从学生信息子组件获取数据
    getStudentInfo (data) {
      if (data === 'no') {
        this.allowSubmitStud = false
      } else {
        this.studentInfo = data
        this.allowSubmitStud = true
      }
    },
    // 将组件中定义的key转化为要传给后台的key
    getIdenKey (key) {
      switch (key) {
        case 'endDate':
          return 'id_end'
        case 'startDate':
          return 'id_start'
        case 'birthDate':
          return 'birth_date'
        case 'currentAddress':
          return 'now_address'
        case 'frontOfIDCard':
          return 'file'
        case 'identityAddress':
          return 'id_address'
        case 'identityNumber':
          return 'id_num'
        case 'firstName':
          return 'name'
        case 'lastName':
          return 'surname'
        case 'sex':
          return 'sex'
        default:
          return key
      }
    },
    getStudKey (key) {
      switch (key) {
        case 'endDate':
          return 'student_card_end'
        case 'startDate':
          return 'student_card_start'
        case 'education':
          return 'education'
        case 'enrolledDate':
          return 'entry_date'
        case 'grade':
          return 'grade'
        case 'institute':
          return 'college'
        case 'major':
          return 'major'
        case 'photo':
          return 'file'
        case 'photoSource':
          return 'student_card_type'
        case 'school':
          return 'school_name'
        case 'schoolDistrict':
          return 'school_district'
        case 'studentID':
          return 'studentID'
        default:
          return key
      }
    },
    // 请求 set 身份认证
    setIden (identityData) {
      this.$ajax.proveData(identityData).then(res => {
        if (res.code === 200) {
          console.log('上传成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log('setIden is wrong')
        }
      })
    },
    // 请求 set 学校认证
    setStud (studentData) {
      this.$ajax.proveData(studentData).then(res => {
        if (res.code === 200) {
          console.log('上传成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log('setStud is wrong')
        }
      })
    },
    // 请求 set 身份认证 & update 学校认证
    setIdenUpdateStud (identityData, studentData) {
      this.$http.all([this.$ajax.proveData(identityData), this.$ajax.updateIdentity(studentData)])
      .then(this.$http.spread((resIden, resStud) => {
        // 两个请求现在都执行完成
        if (resIden.code === 200 && resStud.code === 200) {
          console.log('上传成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log('something is wrong')
        }
      }))
    },
    // 请求 update 身份认证 & set 学校认证
    updateIdenSetStud (identityData, studentData) {
      this.$http.all([this.$ajax.updateIdentity(identityData), this.$ajax.proveData(studentData)])
      .then(this.$http.spread((resIden, resStud) => {
        // 两个请求现在都执行完成
        if (resIden.code === 200 && resStud.code === 200) {
          console.log('上传成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log('something is wrong')
        }
      }))
    },
    // 请求 update 身份认证的方法
    updateIdenConfirm (identityData) {
      this.$ajax.updateIdentity(identityData).then((res) => {
        // 请求现在执行完成
        if (res.code === 200) {
          console.log('上传身份认证信息成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log(res.msg)
        }
      })
    },
    // 请求 update 学校认证的方法
    updateStudConfirm (studentData) {
      this.$ajax.updateIdentity(studentData).then((res) => {
        // 请求现在执行完成
        if (res.code === 200) {
          console.log('上传学校认证信息成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log(res.msg)
        }
      })
    },
    // 封装请求 update 身份认证 & 学校认证的方法
    updateDoubleConfirm (identityData, studentData) {
      this.$http.all([this.$ajax.updateIdentity(identityData), this.$ajax.updateIdentity(studentData)])
      .then(this.$http.spread((resIden, resStud) => {
        // 两个请求现在都执行完成
        if (resIden.code === 200 && resStud.code === 200) {
          console.log('上传成功')
          this.progress = 3
          // 跳转到身份信息页面
          this.toIdentity()
        } else {
          console.log('something is wrong')
        }
      }))
    }
  },
  created () {
    if (this.type === 'update') {
      this.getUpdateData()
    } else if (this.type === 'change') {
      this.getChangeData()
    } else {
      console.log('set')
    }
  },
  mounted () {
    window.addEventListener('popstate', () => {
      this.progress = 1
    })
    window.scrollTo(0, 0)

    // 初始化 set 状态
    if (this.setType === 'idenExists') {
      this.progress = 2
    }
  }
}
</script>

<style scoped>
.identity-authentication {
	min-width: 1200px;
	background-color: #f5f5f5;
	padding-bottom: 40px;
}

.identity-authentication-container {
	width: 1200px;
	margin: 0 auto;
}

.identity-authentication-container .breadcrumb {
  padding-top: 20px;
  overflow: hidden;
}

.identity-authentication-container .breadcrumb li {
  float: left;
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  color: #666;
}

.identity-authentication-container .breadcrumb a {
  color: #666;
}

.identity-authentication-container .breadcrumb a:after {
  content: "\003e";
  display: inline-block;
  height: 20px;
  padding: 0 4px;
}

.authentication {
	width: 1200px;
	background-color: #fff;
	margin-top: 20px;
	padding: 60px 0 80px;
}

.authentication-title {
	height: 33px;
	line-height: 33px;
	font-size: 24px;
	font-weight: bold;
	text-align: center;
}

.authentication-warning {
	width: 805px;
	height: 50px;
	margin: 40px auto 0;
	line-height: 50px;
	font-size: 14px;
	border: 1px solid #ff600a;
	padding-left: 55px;
	background: #fffdf4 url(../../../assets/images/icon_errorTips.png) no-repeat 23px center;
}

.authentication-content {
	margin-top: 60px;
}

.v-identity-component {
  width: 861px;
  margin: 0 auto;
}

.v-student-component {
  width: 861px;
  margin: 0 auto;
}

.upload-item {
	width: 861px;
	margin: 0 auto 80px;
}

.authentication-tip {
	height: 20px;
	line-height: 20px;
	font-size: 14px;
	color: #ff600a;
	margin-top: 12px;
	padding-left: 24px;
	background: url(../../../assets/images/icon_errorTips.png) no-repeat left center;
}

.btn-submit {
	height: 48px;
  margin-top: 80px;
	text-align: center;
}

.btn-submit > button {
	width: 376px;
	height: 48px;
	border: none;
	background-color: #ff600a;
	color: #fff;
	border-radius: 2px;
	cursor: pointer;
}

.btn-forbid-item {
  background-color: #d7d7d7 !important;
  cursor: not-allowed !important;
}

.identity-progress-bar {
  margin-top: 60px !important;
}

.identity-progress-text {
  width: 600px;
  margin: 20px auto 60px;
  overflow: hidden;
}

.identity-progress-text li {
  float: left;
  width: 200px;
  font-size: 16px;
  line-height: 22px;
  text-align: center;
}

.change-success {
  width: 480px;
  margin: 0 auto;
  padding: 60px 0;
}

.change-success-icon {
  width: 60px;
  height: 60px;
  margin: 0 auto;
  background: url(../../../assets/images/icon_success.png) no-repeat center;
}

.change-success-title {
  height: 26px;
  line-height: 26px;
  font-size: 24px;
  color: #333;
  text-align: center;
  margin-top: 24px;
}

.change-success-tip {
  height: 20px;
  line-height: 20px;
  font-size: 14px;
  color: #747474;
  text-align: center;
  margin-top: 12px;
}
</style>